package com.huida.app.hutoolsusage;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import org.apache.commons.collections.ListUtils;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * @author wangzhuangzhuang
 * @DESC: 列表工具
 * @date 2022-01-11 10:53
 * List在集合中使用最为频繁，
 */
@SpringBootTest
public class ListUtil {

	@Test
	public void test01(){
		LinkedList<String> arr =
				cn.hutool.core.collection.ListUtil.toLinkedList("1", "2", "3");
	}

	@Test
	public void test02(){
		LinkedList<String> strings = cn.hutool.core.collection.ListUtil.toLinkedList("1", "2", "3", "4", "3", "2", "1");
		int[] indexArray = cn.hutool.core.collection.ListUtil.indexOfAll(strings, "2"::endsWith);
		// [1, 5]
		System.out.println(ArrayUtil.toString(indexArray));
	}

	@Test
	public void test03(){
		List<List<Integer>> split = cn.hutool.core.collection.ListUtil.split(Arrays.asList(1, 2, 3, 4), 1);
		// [[1], [2], [3], [4]]
		System.out.println(ArrayUtil.toString(split));
		List<List<Object>> split1 = cn.hutool.core.collection.ListUtil.split(null, 3);
		// []
		System.out.println(ArrayUtil.toString(split1));

		List<List<Integer>> lists = cn.hutool.core.collection.ListUtil.splitAvg(Arrays.asList(1, 2, 3, 4), 1);
		// lists: [[1, 2, 3, 4]]
		System.out.println("lists: " + ArrayUtil.toString(lists));
		List<List<Integer>> lists1 = cn.hutool.core.collection.ListUtil.splitAvg(Arrays.asList(1, 2, 3, 4), 3);
		// lists1: [[1, 2], [3], [4]]
		System.out.println("lists1: " + ArrayUtil.toString(lists1));
	}

	@Test
	public void test04(){
		LinkedList<String> arr = cn.hutool.core.collection.ListUtil.toLinkedList("1", "2", "3");
		final List<String> filteredArr = (List<String>) CollUtil.edit(arr, str -> "edit" + str);
		System.out.println("filteredArr: " + ArrayUtil.toString(filteredArr));
	}

	@Test
	public void test05(){
		LinkedList<String> arr = cn.hutool.core.collection.ListUtil.toLinkedList("1", "2", "3", "4", "3", "2", "1");
		// 查找所有2的位置
		// [1, 5]
		int[] ints = cn.hutool.core.collection.ListUtil.indexOfAll(arr, "2"::equals);
		System.out.println(ArrayUtil.toString(ints)); // [1, 5]
	}


	// 元素交换
	@Test
	public void tst06(){
		List<Integer> list = Arrays.asList(7, 2, 8, 9);


		// 将元素8和第一个位置交换
		cn.hutool.core.collection.ListUtil.swapTo(list, 8, 1);

		// list: [7, 8, 2, 9]
		System.out.println("list: " + ArrayUtil.toString(list));

	}


}